const vmVersion = weex.requireModule('vmVersion')

const Utils = Object.create(null)
Utils.install = Vue => {
  Vue.prototype.$version = {
    /**
     * 获取App版本信息
     * VersionInfo {
     *   firstInstallTime: long
     *   versionName: string
     *   versionCode: int
     *   packageName: string
     *   channel: string
     * }
     */
    getVersionInfo() {
      return new Promise((resolve, reject) => {
        vmVersion.getVersionInfo({}, ({ status, errorMsg, data }) => {
          status === 0 ? resolve(data) : reject({ status, errorMsg, data })
        })
      })
    },

    /**
     * 下载并安装APP
     */
    downloadAndInstall({ downloadUrl = '', versionTips = '下载更新' }) {
      return new Promise((resolve, reject) => {
        if (!downloadUrl) {
          reject({ status: 9, errorMsg: '没有下载地址', data: {} })
        }
        vmVersion.downloadAndInstall({ downloadUrl, versionTips }, ({ status, errorMsg, data }) => {
          status === 0 ? resolve(data) : reject({ status, errorMsg, data })
        })
      })
    }
  }
}

Vue.use(Utils)